package com.example.beautyhub.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.beautyhub.model.Employees;
import com.example.beautyhub.model.OrderInfo;
import com.example.beautyhub.model.Orders;
import com.example.beautyhub.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(tags = "员工")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private OrderController orderController;

    @GetMapping("/")
    public List<Employees> list() {
        return employeeService.list();
    }

    @ApiOperation("分页查询")
    @GetMapping("/list")
    public Page<Employees> getEmployeeList(int pageNum, int pageSize, Employees employees) {
        Page<Employees> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Employees> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(employees.getName())) {
            wrapper.like("name", employees.getName()); //根据条件查询
        }
        page.setTotal(employeeService.count(wrapper));
        return employeeService.page(page, wrapper);//返回分页数据
    }

    @ApiOperation("离职操作")
    @GetMapping("/leave/{id}")
    public void getLeaveId(@PathVariable Integer id) {
        Employees employees = findById(id);
        employees.setStatus("离职");
        employeeService.updateById(employees);
    }

    @ApiOperation("在职员工")
    @GetMapping("/in")
    public List<Employees> getEmployeeInList() {
        QueryWrapper<Employees> wrapper = new QueryWrapper<>();
        wrapper.eq("status", "在职");
        return employeeService.list(wrapper);
    }

    @ApiOperation("离职员工")
    @GetMapping("/out")
    public List<Employees> getEmployeeOutList() {
        QueryWrapper<Employees> wrapper = new QueryWrapper<>();
        wrapper.eq("status", "离职");
        return employeeService.list(wrapper);
    }

    @ApiOperation("登陆")
    @PostMapping("/login")
    public int login(Employees employees) {
        // 获取前端传入的原始密码，并进行加密
        String userPassword = employees.getPassword();
        // 使用 LambdaQueryWrapper 查询符合条件的加密后的密码
        LambdaQueryWrapper<Employees> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employees::getUsername, employees.getUsername());
        List<Employees> userList = employeeService.list(wrapper);
        // 对查询结果进行密码校验
        if (userList != null && userList.size() > 0) {
            String dbEncodedPassword = userList.get(0).getPassword();
            if (userPassword.equals(dbEncodedPassword)) {
                // 密码匹配
                if (userList.get(0).getType().equals("admin"))
                    return 1;
                else
                    return 0;
            } else {
                // 密码不匹配
                return -1;
            }
        } else {
            // 用户不存在
            return -2;
        }
    }

    @GetMapping("/{id}")
    public Employees findById(@PathVariable Integer id) {
        return employeeService.getById(id);
    }

    @ApiOperation("员工个人信息")
    @GetMapping("/byId")
    public Employees find(String username) {
        QueryWrapper<Employees> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        List<Employees> employeesList = employeeService.list(queryWrapper);
        return employeeService.getById(employeesList.get(0).getId());
    }

    @PostMapping("/")
    public void add(@RequestBody Employees employee) {
        employee.setEntryTime(new Date());
        employee.setStatus("在职");
        employee.setSize(0);
        employee.setPassword("123");
        employeeService.save(employee);
    }

    @PutMapping("/{id}")
    public void update(@PathVariable Integer id, @RequestBody Employees employee) {
        employee.setId(id);
        employeeService.updateById(employee);
    }

    @ApiOperation("数据继承 employee为实体类为新员工即继承人信息 id为离职员工即被继承人的id")
    @PutMapping("/data/{id}")
    public void dateId(@PathVariable Integer id, @RequestBody Employees employee) {
        Integer old = employee.getId();
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("employee_id",id);
        List<Orders> ordersList = orderController.getOrdersList(queryWrapper);
        for (Orders orders : ordersList) {
            orders.setEmployeeId(old);
            orderController.update(orders.getId(),orders);
        }
        delete(id);
        employee.setId(id);
        employeeService.updateById(employee);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable Integer id) {
        employeeService.removeById(id);
    }
}

